//
//  BeadsMainController.m
//  Beads
//
//  Created by CPX on 2018/10/31.
//  Copyright © 2018 cpx. All rights reserved.
//

#import "BeadsMainController.h"
#import "TasbihListController.h"
#import "TasbihHistoryController.h"

@interface BeadsMainController ()
<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>
@property (weak, nonatomic) IBOutlet UIButton *resetBt;/**<充值按钮 */
@property (weak, nonatomic) IBOutlet UILabel *currentCountLabel;/**<当前拨的念珠总数 */
@property (weak, nonatomic) IBOutlet UIView *tasbihView;

@property (weak, nonatomic) IBOutlet UILabel *proverbLabel;//经文label

@property (weak, nonatomic) IBOutlet UIView *centerContainer;/**<中间那块方块的区域背景图 */
@property (weak, nonatomic) IBOutlet UICollectionView *bottomCollectionView;/**<下面念珠的配色图 */

@property (nonatomic,strong)NSMutableArray * dataSourceImage;/**<下面collection 数据源 */

@property (nonatomic,strong)UIImage  * currentImage;//当前c选中珠子图
@property (nonatomic,strong)NSString * currentProverb;//当前经文

@property (nonatomic,assign)NSInteger touchReponseType;/**<滑动念珠手机响应类型 0 -- 声音 1 -- 震动 2 -- 无响应、既不震动也无声音 */
@property (nonatomic,assign)NSInteger totalType;/**<当前念珠总数类型 0 -- 99 1 -- 33 */




@end

@implementation BeadsMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
    // Do any additional setup after loading the view from its nib.
}
- (void)initViews{
    [self setupCenterContainer];
    [self setupBottomCollectionView];
    [self setDefalutimageSelect];
}
- (void)loadData{
    [TasbihListController getDefaultProverbtext:^(NSString * _Nonnull proverb_text_default) {
        self.proverbLabel.text = proverb_text_default;
        self.currentProverb = proverb_text_default;
    }];
}

- (void)initNavigation{
    [self initDefaultNavigation];
    self.myNavigationBar.backgroundColor  = UIColor.clearColor;
    [self.myNavigationBar.backButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [self.myNavigationBar.backButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.myNavigationBar.backButton setTitle:@"Rosary" forState:UIControlStateNormal];

    [self.myNavigationBar.rightButton1 addTarget:self action:@selector(soundSwitch:) forControlEvents:UIControlEventTouchUpInside];
    [self.myNavigationBar.rightButton2 addTarget:self action:@selector(totalTimeSwitch:) forControlEvents:UIControlEventTouchUpInside];

    [self.myNavigationBar.backButton setImage:[UIImage imageNamed:@"back_ic_white"] forState:UIControlStateNormal];
  
    self.touchReponseType = 0;
    self.totalType = 0;
    
}
#pragma mark ---设置中间那块区域
-(void)setupCenterContainer{
    self.centerContainer.layer.shadowColor = UIColor.lightGrayColor.CGColor;
    self.centerContainer.layer.shadowOffset = CGSizeMake(5, 5);
    self.centerContainer.layer.shadowOpacity = 0.5;
    self.centerContainer.layer.cornerRadius = 5;
    self.tasbihView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tasbihViewTap:)];
    [self.tasbihView addGestureRecognizer:tap];
}
#pragma mark -- 设置下面的那个配色collectionview
-(void)setupBottomCollectionView{
    self.bottomCollectionView.delegate = self;
    self.bottomCollectionView.dataSource = self;
    self.bottomCollectionView.backgroundColor=UIColor.clearColor;
    [self.bottomCollectionView registerClass:[BeadsSelectCell class] forCellWithReuseIdentifier:NSStringFromClass(BeadsSelectCell.class)];
    CGFloat width =  (kScreenWidth  - 8*15) / 7;
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(width, width);
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 15;
    self.bottomCollectionView.collectionViewLayout = layout;
}
-(void)setDefalutimageSelect{
    NSIndexPath  *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.bottomCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    self.currentImage = self.dataSourceImage.firstObject;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSourceImage.count;
}


- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BeadsSelectCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(BeadsSelectCell.class) forIndexPath:indexPath];
    UIImage *bg = self.dataSourceImage[indexPath.row];
    cell.bg.image = bg;
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}




#pragma mark --- event
-(void)soundSwitch:(UIButton *)sender{
    self.touchReponseType += 1;
    if (self.touchReponseType == 3){
        self.touchReponseType = 0;
    }
    
}

-(void)totalTimeSwitch:(UIButton *)sender{
    self.totalType += 1;
    if (self.totalType == 2){
        self.totalType = 0;
    }
}
//充值当前珠子
- (IBAction)resetCurrentBeads:(UIButton *)sender {
}

-(void)tasbihViewTap:(UITapGestureRecognizer*)ges{
    TasbihListController  * taslihVC =  [TasbihListController new];
    @weakify(self);
    taslihVC.proverbTextSelectBlock = ^(NSString * _Nonnull proverb_text) {
        self.currentProverb = proverb_text;
        self.proverbLabel.text = self.currentProverb;
    };
    [self.navigationController pushViewController:taslihVC animated:YES];
}
//去经文日志
- (IBAction)showLog:(UIButton *)sender {
    [self.navigationController pushViewController:[TasbihHistoryController new] animated:YES];
    
}

#pragma mark ----- getter
- (NSMutableArray *)dataSourceImage{
    if (!_dataSourceImage){
        _dataSourceImage = [NSMutableArray new];
        for (NSInteger index = 0;index<7;index++) {
            NSString *name = [NSString stringWithFormat:@"beads_img%d",index + 1];
            [_dataSourceImage addObject:[UIImage imageNamed:name]];
        }
    }
    return _dataSourceImage;
}

- (void)setTouchReponseType:(NSInteger)touchReponseType{
    _touchReponseType = touchReponseType;
    switch (touchReponseType) {
        case 0:
        {
            [self.myNavigationBar.rightButton1 setImage:[UIImage imageNamed:@"tasbeeh_ring"] forState:UIControlStateNormal];
            break;
        }
        case 1:
        {
            [self.myNavigationBar.rightButton1 setImage:[UIImage imageNamed:@"tasbeeh_shake"] forState:UIControlStateNormal];
            break;
        }
        case 2:
        {
            [self.myNavigationBar.rightButton1 setImage:[UIImage imageNamed:@"tasbeeh_no_ring"] forState:UIControlStateNormal];
            break;
        }
            
        default:
            break;
    }
}

- (void)setTotalType:(NSInteger)totalType{
    _totalType = totalType;
    switch (totalType) {
        case 0:
        {
            [self.myNavigationBar.rightButton2 setImage:[UIImage imageNamed:@"beads_99"] forState:UIControlStateNormal];
            break;
        }
        case 1:
        {
            [self.myNavigationBar.rightButton2 setImage:[UIImage imageNamed:@"beads_33"] forState:UIControlStateNormal];
            break;
        }
       
        default:
            break;
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end






@implementation BeadsSelectCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
-(void)createView{
    self.highlighted = NO;
    self.bg = [UIImageView new];
    [self.contentView addSubview:_bg];
    [_bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    _status  = [UIImageView new];
    [self.contentView addSubview:_status];
    [_status mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_bg);
    }];
}
- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    self.selected ? (self.status.image=[UIImage imageNamed:@"beads_s_ic"]) : (self.status.image = nil);
    
}

@end
